﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Net;

namespace UpdateBot2.DownloadSource
{
    abstract class DownloadServer
    {

        /// <summary>
        /// Performs search for the specified query; return length is limited by the specified limit.
        /// </summary>
        /// <param name="programName"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public abstract List<DownloadResourceIdentifier> Search(String programName, int limit);

        /// <summary>
        /// Retreives resources 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public abstract DownloadTarget GetDownloadTarget(DownloadResourceIdentifier id);

        /// <summary>
        /// Downloads the file using this instance's WebClient and passes it the DownloadTarget as the
        /// user token.
        /// </summary>
        /// <param name="d"></param>
        public abstract void DownloadTargetAsync(DownloadTarget d, DirectoryInfo dir);

        /// <summary>
        /// Translates bytes to kb, mb, gb, tb respectively.
        /// </summary>
        /// <param name="numBytes"></param>
        /// <returns></returns>
        public static String BytesToString(long numBytes)
        {
            long kb = numBytes / 1024;
            long mb = kb / 1024;
            long tb = mb / 1024;

            if (tb > 1)
            {
                return String.Format("{0:0.##} TB", tb);
            }
            else if (mb > 1)
            {
                return String.Format("{0:0.##} MB", mb);
            }
            else if (kb > 1)
            {
                return String.Format("{0:0.##} KB", kb);
            }
            else
            {
                return String.Format("{0:0.##} bytes", numBytes);
            }
        }
    }

    /// <summary>
    /// Indetifies download on server.
    /// Possibilities may include numerical ID or URL.n m
    /// </summary>
    class DownloadResourceIdentifier
    {
        /// <summary>
        /// Unique string indentifying this resource.
        /// </summary>
        public String ID { get; set; }

        /// <summary>
        /// Constructor setting up resource values.
        /// </summary>
        /// <param name="id"></param>
        public DownloadResourceIdentifier(String id)
        {
            this.ID = id;
        }
    }

    /// <summary>
    /// This is the token represented in. 
    /// </summary>
    class DownloadServerDownloadToken
    {
        public DownloadTarget Target { get; set; }

        public DownloadServerDownloadToken(DownloadTarget t)
        {
            this.Target = t;
        }
    }
}
